package com.koron.order.prodForm.controller;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.koron.bean.base.Response;
import com.koron.bean.query.PageQuery;
import com.koron.bean.system.post.query.PostUserQuery;
import com.koron.bean.system.post.vo.PostUserVo;
import com.koron.bean.workflow.RedirectQuery;
import com.koron.bean.workflow.SubmitterParam;
import com.koron.bean.workflow.WorkflowController;
import com.koron.bean.workflow.WorkflowUser;
import com.koron.common.core.business.system.feign.SystemFeignService;
import com.koron.common.core.business.workflow.templates.WorkFlowHandler;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.CommonUtils;
import com.koron.order.prodForm.bean.po.ProdForm;
import com.koron.order.prodForm.bean.query.ProdFormQuery;
import com.koron.order.prodForm.service.ProdFormService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 生产报表(ProdForm)表控制层
 *
 * @author jzp
 * @since 2022-08-10 15:27:52
 */
@Api(tags = "生产报表接口")
@RestController
@RequestMapping("prodForm")
@Slf4j
public class ProdFormController implements WorkflowController {
    /**
     * 服务对象
     */
    @Autowired
    private ProdFormService prodFormService;

    @Autowired
    private SystemFeignService systemFeignService;

    @Autowired
    private WorkFlowHandler workFlowHandler;

    private String getCurrDs() {
        return ThreadLocalContext.get().getCurrDs();
    }

    @ApiOperation(value = "分页")
    @GetMapping("page")
    public Response<PageInfo<ProdForm>> page(PageQuery pageQuery, ProdFormQuery query) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), prodFormService, "page", PageInfo.class, pageQuery, query));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "根据id查询")
    @GetMapping
    public Response<ProdForm> selectOne(@RequestParam String id) {
        return Response.ok(ADOConnection.runTask(getCurrDs(), prodFormService, "getById", ProdForm.class, id));
    }

    @ApiOperation(value = "保存或更新数据")
    @PostMapping
    public Response<String> save(@RequestBody ProdForm prodForm) {
        ADOConnection.runTask(getCurrDs(), prodFormService, "save", Boolean.class, prodForm);
        return Response.ok(prodForm.getId());
    }

    @ApiOperation(value = "删除数据")
    @DeleteMapping
    public Response<String> delete(@RequestParam String id) {
        ADOConnection.runTask(getCurrDs(), prodFormService, "deleteById", Boolean.class, id);
        workFlowHandler.delete(id);
        return Response.ok();
    }

    @Override
    public Response<Object> updateState(RedirectQuery redirectQuery) {
        log.info("工作流更新: {}", JSON.toJSONString(redirectQuery));
        // 撤回或者废弃
        ProdForm prodForm = new ProdForm();
        prodForm.setId(redirectQuery.getDataId());
        if (redirectQuery.getOperation() == 1 || redirectQuery.getOperation() == 256) {
            // 修改为草稿状态
            prodForm.setStatus(ProdFormService.ProdFormStatus.DRAFT.getCode());
        } else {
            prodForm.setStatus(ProdFormService.ProdFormStatus.WAIT_REVIEW.getCode());
        }
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), prodFormService, "update", Boolean.class, prodForm);
        return Response.success();
    }

    @Override
    public Response<Object> updateEnd(RedirectQuery redirectQuery) {
        log.info("工作流完结: {}", JSON.toJSONString(redirectQuery));
        ProdForm prodForm = new ProdForm();
        prodForm.setId(redirectQuery.getDataId());
        prodForm.setStatus(ProdFormService.ProdFormStatus.ARCHIVED.getCode());
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), prodFormService, "update", Boolean.class, prodForm);
        return Response.success();
    }

    @Override
    public Response<Object> updateCancel(RedirectQuery redirectQuery) {
        log.info("业务单: {}, 工作流节作废", JSON.toJSONString(redirectQuery));
        ProdForm prodForm = new ProdForm();
        // 修改为草稿状态
        prodForm.setId(redirectQuery.getDataId());
        prodForm.setStatus(ProdFormService.ProdFormStatus.DEPRECATED.getCode());
        ADOConnection.runTask(ThreadLocalContext.get().getCurrDs(), prodFormService, "update", Boolean.class, prodForm);
        // 删除工作流
        // workFlowHandler.delete(redirectQuery.getDataId());
        return Response.success();
    }

    @PostMapping("/task/submitter")
    public Response<List<WorkflowUser>> submitter(@RequestBody SubmitterParam submitterParam) {
        log.info("生产报表申请审批设置审批人入参: {}", JSON.toJSONString(submitterParam));
        ProdForm prodForm = JSON.parseObject(submitterParam.getFormVariables(), ProdForm.class);
        PostUserQuery query = new PostUserQuery();
        // 部门副总经理
        query.setPostIdList(Arrays.asList(submitterParam.getUsers().get(0).getCode()));
        query.setOrgIdList(Arrays.asList(prodForm.getDeptId()));
        List<PostUserVo> vos = CommonUtils.ok(systemFeignService.queryPostList(query));
        List<WorkflowUser> users = new ArrayList<>();
        vos.stream().forEach(vo -> {
            WorkflowUser user = new WorkflowUser();
            user.setCode(vo.getUserAccount());
            user.setName(vo.getUserName());
            users.add(user);
        });
        if (users.size() == 0) {
            return null;
        }
        return Response.ok(users);
    }

}

